'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
    // 给数据库中增加一条数据
    // 在增加之前数据库中必须已经有该表和相关的字段名
    async create() {
        const { ctx, app } = this;
        // const data = ctx.request.body.username;
        const res = await app.mysql.insert('test', { username: "root", password: 123456, id: 2 });
        //   返回数据
        ctx.body = res;
    }
    //   从数据库中读取数据
    async read() {
        const { ctx, app } = this;
        const res = await app.mysql.get('test', { username: "zhangsan" });
        ctx.body = res;
    }
    // 更新数据库的内容
    async update() {
        const { ctx, app } = this;
        // 在更新的时候 id作为主键是查询的关键
        const row = {
            id: 4,
            username: 'zhangsan',
            password: 123456789,
            // modifiedAt: app.mysql.literals.now,
          };
        const result = await app.mysql.update('test', row);
    }
    // 删除用户的数据
    async delete() {
        const { ctx, app } = this;

        const result = await app.mysql.delete('test', {
            username: 'zhangsanf20',
        });
    }



}

module.exports = HomeController;